//
//  CityChooserTableViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/20.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "CityChooserTableViewModel.h"

#import "CityChooserCellModel.h"

@interface CityChooserTableViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation CityChooserTableViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = NO;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = NO;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = NO;
    
    /// 请求数据
    self.requestDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id areaID) {
        
        @strongify(self);
        
        return [self requestListDataSignal:areaID];
    }];
    
    /// 选中
    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id x) {
        @strongify(self);
        
        @weakify(self);
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            @strongify(self);
            
            @weakify(self);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^
                           {
                               @strongify(self);
                               [subscriber sendNext:x];
                               [subscriber sendCompleted];
                           });
            return nil;
        }];
    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
    
    self.areaID = @"";
    [[RACObserve(self, areaID) distinctUntilChanged] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [self.requestDataCmd execute:x];
    }];
}

- (RACSignal *)requestListDataSignal:(NSString*)areaID
{
    @weakify(self)
    
    DataManager* dm = [DataManager sharedDataManager];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        @strongify(self);
        
        BOOL isSubCell = NO;
        NSArray* provinceDatas = @[];
        
        if ([areaID isNotBlank])
        {
            if ([areaID isEqualToString:@"0"])
            {
                provinceDatas = self.currentAddrDatas.datas;
            }
            else
            {
                ProvinceListData* pListData = [dm findProvinceDataBy:self.currentAddrDatas andAreaID:areaID];
                provinceDatas = pListData.datas;
                
                isSubCell = YES;
            }
        }
        
        @weakify(self);
        NSArray* viewModels = [provinceDatas.rac_sequence map:^id(ProvinceListData* pListData)
        {
            @strongify(self);
            CityChooserCellModel* cellModel = [[CityChooserCellModel alloc] initWithData:pListData cellHeight:self.cellHeight];
            cellModel.isSub = isSubCell;
            
            return cellModel;
        }].array;
        
        self.dataSource = viewModels;
        
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        
        return nil;
    }];
}

@end
